﻿using Caliburn.Micro;
using System;
using System.Security.Permissions;
using System.Threading;
using System.Windows.Threading;
using ThunderboltInterfaceSharp.ViewModels;

namespace ThunderboltInterfaceSharp;

public class ThunderStartBackgroundWorker: SimpleBackgroundWorker
{
	protected override void Process()
	{
		base.Process();
		for (int i = 0; i <= 10; i++)
		{
			Worker.ReportProgress(i, "Be starting");
			Delay(30);
		}
	}

	protected override void Complete()
	{
		base.Complete();
	}
	/// <summary>
	/// 延时函数
	/// </summary>
	/// <param name="millSecond">毫秒数</param>
	public static void Delay(int millSecond)
	{
		var start = Environment.TickCount;
		while (Math.Abs(Environment.TickCount - start) < millSecond)//毫秒
		{
			DispatcherHelper.DoEvents();
		}
	}
	public static class DispatcherHelper
	{
		[SecurityPermissionAttribute(SecurityAction.Demand, Flags = SecurityPermissionFlag.UnmanagedCode)]
		public static void DoEvents()
		{
			DispatcherFrame frame = new DispatcherFrame();
			Dispatcher.CurrentDispatcher.BeginInvoke(DispatcherPriority.Background, new DispatcherOperationCallback(ExitFrames), frame);
			try { Dispatcher.PushFrame(frame); }
			catch (InvalidOperationException) { }
		}
		private static object ExitFrames(object frame)
		{
			((DispatcherFrame)frame).Continue = false;
			return null;
		}

	}
}